package com.zhongwang.mms.module.jiaGongSemi.controller;

import com.zhongwang.mms.base.BaseController;
import com.zhongwang.mms.common.JsonResult;
import com.zhongwang.mms.gen.entity.User;
import com.zhongwang.mms.module.half.model.HOrderDetailModel;
import com.zhongwang.mms.module.half.service.HBasicService;
import com.zhongwang.mms.module.half.service.RedOrderDetailService;
import com.zhongwang.mms.module.jiaGongSemi.model.JSemiDetail;
import com.zhongwang.mms.module.jiaGongSemi.service.OffsetService;
import com.zhongwang.mms.module.jiaGongSemi.service.StockQueryService;
import com.zhongwang.mms.util.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @description:红冲
 * @author: th
 * @time: 2020/4/14 15:44
 */
@Controller
@RequestMapping("/offset")
public class OffsetController extends BaseController {


    @Autowired
    private RedOrderDetailService redOrderDetailService;
    @Autowired
    private OffsetService offsetService;
    @Autowired
    private StockQueryService stockQueryService;

    //页面
    @GetMapping("/page")
    public String create(Model model) {

        // 获取登入用户实体
        User user = ShiroUtils.getUser();
        model.addAttribute("userName", user.getName());
        model.addAttribute("odrDate", new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        model.addAttribute("warInfo", stockQueryService.getFinishWarInfo());
        return "jiaGongSemi/offset/page";
    }
    //明细列表
    @ResponseBody
    @PostMapping("/list")
    public JsonResult stockList(JSemiDetail query){

        startPage();
        return jsonResult(offsetService.selectAllByQuery(query));
    }

    //单条红冲
    @ResponseBody
    @PostMapping("/toRed")
    public JsonResult toRed(Integer odaId){

        JsonResult jsonResult = new JsonResult();
        if(offsetService.updateDetailToRedByOdaId(odaId) == 0){
            jsonResult.setType(JsonResult.ResultType.ERROR);
            jsonResult.setMsg("库存数量不足，无法冲单！");
            return jsonResult;
        }else {
            return JsonResult.success();
        }
    }

    //批量红冲
    @ResponseBody
    @PostMapping("/toRedTogether")
    public JsonResult toRedTogether(@RequestBody List<JSemiDetail> hOrderDetailModels){

        JsonResult jsonResult = new JsonResult();
        String msg = offsetService.toRedTogether(hOrderDetailModels);
        if(msg.equals("全部选中") || msg.equals("正常批量红冲")){
            jsonResult.setType(JsonResult.ResultType.SUCCESS);
            return jsonResult;
        }else {
            jsonResult.setType(JsonResult.ResultType.ERROR);
            jsonResult.setMsg(msg);
            return jsonResult;
        }
    }
}
